package com.zhuxin.events;

import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxMessageHandler;
import com.soecode.wxtools.bean.WxXmlMessage;
import com.soecode.wxtools.bean.WxXmlOutMessage;
import com.soecode.wxtools.exception.WxErrorException;
import com.zhuxin.medical.model.po.CommWxUser;

import java.util.List;
import java.util.Map;

/**
 * Created by lzb on 2018/11/13.
 */
public class AdminMsgHandler extends BaseHandler implements WxMessageHandler {
    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, IService iService) throws WxErrorException {
        WxXmlOutMessage xmlOutMsg = null;

        String[] msg=wxMessage.getContent().split(" ");
        String rsmsg="";
        switch (msg[0]){
            case "userlist":
                List<CommWxUser> userlist=this.mysqlDao.querySub("select * from commWxUser", CommWxUser.class);
                StringBuffer sb=new StringBuffer();
                userlist.forEach(user->sb.append(user.getNickname()).append("-->").append(user.getUsername()).append("\n"));
//                xmlOutMsg=WxXmlOutMessage.TEXT().content(sb.toString()).toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
                rsmsg=sb.toString();
                break;
            case "usertype":
                this.mysqlDao.update("update commWxUser set usertype=? where username=?",msg[1],msg[2]);
                rsmsg="更新完成.";
                break;
            default:
                rsmsg="指令无效";
        }
        xmlOutMsg=WxXmlOutMessage.TEXT().content(rsmsg).toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
        return xmlOutMsg;
    }
}
